package cn.fufeiii.bt.easyexcel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.Objects;
import java.util.function.Consumer;

/**
 * 单个数据读取监听器
 *
 * @author FuFei
 */
public class SingleReadListener<T> extends AnalysisEventListener<T> {

    private final Consumer<T> dataAccepter;
    private final Consumer<AnalysisContext> analysisContextAccepter;

    public SingleReadListener(Consumer<T> dataAccepter) {
        this(dataAccepter, null);
    }

    public SingleReadListener(Consumer<T> dataAccepter, Consumer<AnalysisContext> analysisContextAccepter) {
        Objects.requireNonNull(dataAccepter);
        this.dataAccepter = dataAccepter;
        this.analysisContextAccepter = analysisContextAccepter;
    }

    @Override
    public void invoke(T data, AnalysisContext context) {
        dataAccepter.accept(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (analysisContextAccepter != null) {
            analysisContextAccepter.accept(context);
        }
    }

}
